//修改人员
var topNavVm = new Vue({
    el: "#topNav",
    data: {
        menus: [],
        topId: ''
    },
    methods: {
        loadMenus: function () {
            VanyAjax("/index/menus", {}, "GET").done(function (data) {
                this.menus = data;
                if (!data || !data.length) {
                    alert("菜单获取异常！");
                }
                if (!window.location.hash) {
                    this.changeMenu(data[0]);
                    return;
                }
                var leftUri = window.location.hash;
                for (var i = 0; i < data.length; i++) {
                    if (!data[i].children || !data[i].children.length) {
                        continue;
                    }
                    for (var j = 0; j < data[i].children.length; j++) {
                        if ("#" + data[i].children[j].uri === leftUri) {
                            this.topId = data[i].id;
                            leftNavVm.menus = data[i].children;
                            leftNavVm.openPage(data[i].children[j]);
                            return;
                        }
                    }
                }
            }, false, this);
        },
        changeMenu: function (menu) {
            this.topId = menu.id;
            leftNavVm.menus = menu.children;
            leftNavVm.openPage(menu.children[0]);
        }
    }
});

var leftNavVm = new Vue({
    el: '#subNavBox',
    data: {
        menus: [],
        leftIndex: '',
        menu: {}
    },
    beforeCreate: function () {
    },
    mounted: function () {
    },
    updated: function () {
        $("#subNavBox .el-menu .el-menu-item").eq(this.leftIndex).addClass("is-active");
    },
    methods: {
        select: function (key, keyPath) {
            console.log("select:", key, keyPath);
        },
        handleOpen: function (key, keyPath) {
            console.log("handleOpen:", key, keyPath);
        },
        handleClose: function (key, keyPath) {
            console.log("handleClose:", key, keyPath);
        },
        openPage: function (menu) {
            leftNavVm.menu = menu;
            $("#subNavBox .el-menu .el-menu-item").removeClass("is-active");
            if (!menu.uri) {
                return;
            }
            for (var i = 0; i < this.menus.length; i++) {
                if (menu.id === this.menus[i].id) {
                    this.leftIndex = i + "";
                }
            }
            window.location.hash = menu.uri;
            var content = $("#content");
            content.css("opacity", 0);
            $.fakeLoadershow(); //加载效果显示
            content.load(menu.uri, function () {
                $.fakeLoaderhide(); //加载效果消失
                setTimeout(function () {
                    content.css("opacity", 1);
                }, 300);
            });
        }
    }
});

topNavVm.loadMenus();

const DICTIONARY = {
    businessTitles: "business_title",
    employeeStatuses: "employee_status"
};

function loadDictionaries(callback){
    let codes = [];
    for (const key in DICTIONARY) {
        codes.push(DICTIONARY[key]);
    }
    VanyAjax("/sys/dictionary/list", {code: codes.join()}, "GET").done(function (data) {
        let dicts = {};
        for (let i = 0; i < data.length; i++) {
            for (const key in DICTIONARY) {
                if (DICTIONARY[key] === data[i].code) {
                    if (!dicts[key]) {
                        dicts[key] = [];
                    }
                    dicts[key].push(data[i]);
                }
            }
        }
        callback(dicts);
    }, false, this);
}
